﻿using STMS.DAL;
using STMS.Models.DModels;
using STMS.Models.VModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace STMS.BLL
{
    public class ProductBLL
    {
        private ProductDAL proDAL = new ProductDAL();
        private ProductStoreDAL psDAL = new ProductStoreDAL();
        private ViewProductStoreDAL vpsDAL = new ViewProductStoreDAL();

        #region 产品信息
        /// <summary>
        /// 条件查询产品列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="lowTemper"></param>
        /// <param name="highTemper"></param>
        /// <param name="showDel"></param>
        /// <returns></returns>
        public List<ProductInfo> GetProductList(string keywords,  bool showDel)
        {
            int isDeleted = showDel ? 1 : 0;
            return proDAL.GetProductList(keywords, isDeleted);
        }

        /// <summary>
        /// 获取所有产品列表  绑定下拉框
        /// </summary>
        /// <returns></returns>
        public List<ProductInfo> GetAllProducts()
        {
            return proDAL.GetAllProducts();
        }

        /// <summary>
        /// 检查指定产品是否已有库存
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public bool IsProductHasStore(int productId)
        {
            int proCount = proDAL.GetProductCount(0, 0, productId);
            if (proCount > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 获取指定仓库中该产品的数量
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public int GetProductCountByStoreId(int storeId, int productId)
        {
            int proCount = proDAL.GetProductCount(storeId, 0, productId);
            return proCount;
        }

        /// <summary>
        /// 获取指定分区中的该产品数量
        /// </summary>
        /// <param name="regionId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public int GetProductCountByRegionIdAndProductId(int regionId, int productId)
        {
            int proCount = proDAL.GetProductCount(0, regionId, productId);
            return proCount;
        }

        /// <summary>
        /// 获取指定分区中存放的产品数量
        /// </summary>
        /// <param name="regionId"></param>
        /// <returns></returns>
        public int GetProductCountByRegionId(int regionId)
        {
            int proCount = proDAL.GetProductCount(0, regionId, 0);
            return proCount;
        }

        /// <summary>
        /// 获取指定的产品信息
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public ProductInfo GetProduct(int productId)
        {
            return proDAL.GetProduct(productId);
        }

        /// <summary>
        /// 判断产品是否已存在
        /// </summary>
        /// <param name="productName"></param>
        /// <returns></returns>
        public bool ExistsProduct(string productName)
        {
            return proDAL.ExistsProduct(productName);
        }

        /// <summary>
        /// 添加产品信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <param name="isGetId"></param>
        /// <returns></returns>
        public bool AddProduct(ProductInfo proInfo)
        {
            return proDAL.AddProduct(proInfo, 0) > 0;
        }

        public int AddProductWithId(ProductInfo proInfo)
        {
            return proDAL.AddProduct(proInfo, 1);
        }

        /// <summary>
        /// 修改产品信息
        /// </summary>
        /// <param name="proInfo"></param>
        /// <returns></returns>
        public bool UpdateProduct(ProductInfo proInfo)
        {
            return proDAL.UpdateProduct(proInfo);
        }

        /// <summary>
        /// 产品删除  真删除 
        /// </summary>
        /// <param name="productId"></param>
        /// <returns>0 删除失败  1  删除成功</returns>
        public bool DeleteProduct(int productId)
        {
            bool bl = proDAL.Delete(productId, 1, 2);
            return bl;
        }

        /// <summary>
        /// 逻辑删除产品信息  假删除
        /// </summary>
        /// <param name="productId"></param>
        /// <returns></returns>
        public int LogicDeleteProduct(int productId)
        {
            int re = 0;
            if (IsProductHasStore(productId))
                re = 2;//表示已有库存
            else
            {
                bool bl = proDAL.Delete(productId, 0, 1);
                if (bl)
                    re = 1;//删除成功   
            }
            return re;
        }

        public string LogicDeleteProducts(List<int> productIds)
        {
            string reStr = "";
            foreach (int id in productIds)
            {
                if (IsProductHasStore(id))
                {
                    if (reStr != "") reStr += ",";
                    //表示已有库存
                    reStr +=id.ToString();
                }
            }
            if (reStr == "")
                reStr = proDAL.DeleteList(productIds, 0, 1) ? "" : "0";
            return reStr;
        }

        /// <summary>
        /// 恢复产品信息
        /// </summary>
        /// <param name="productIds"></param>
        /// <returns></returns>
        public bool RecoverProduct(int productId)
        {
            return proDAL.Delete(productId, 0, 0);
        }
        #endregion

        #region 产品入库

        /// <summary>
        /// 产品入库
        /// </summary>
        /// <param name="psInfo"></param>
        /// <returns></returns>
        public bool InStoreProducts(ProductStoreInfo psInfo)
        {
            bool blStore = false;
            if (psInfo == null)
                throw new Exception("入库信息未初始化！");
            else
            {
                if(psDAL.ExitsStoreRecord(psInfo.ProductId,psInfo.SRegionId))
                {
                    blStore= psDAL.UpdateProductStore(psInfo);
                }
                else
                {
                    blStore = psDAL.AddProductStore(psInfo);
                }
            }
            return blStore;
        }

        /// <summary>
        /// 查询商品库存
        /// </summary>
        /// <param name="storeId"></param>
        /// <param name="regionId"></param>
        /// <param name="productId"></param>
        /// <returns></returns>
        public List<ViewProductStoreInfo> GetProductStoreList(int storeId, int regionId, int productId)
        {
            return vpsDAL.GetProductStoreList(storeId, regionId, productId);
        }
        #endregion
    }
}
